<template>
  <div class="app-container">
      <div class="filter-container">
        <el-input clearable v-model="search.configName" placeholder="名称" style="width: 170px;"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="searchClick()" style="margin-left:10px;">查询</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" 
      border fit highlight-current-row stripe 
      style="width: 100%">
      <el-table-column align="center" label="序" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row._id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="10%" align="center" label="配置名称">
        <template slot-scope="scope">
          <span>
              {{scope.row.configName}}
          </span>
        </template>
      </el-table-column>

      <el-table-column  min-width="10%" align="center" label="值">
        <template slot-scope="scope">
          <span>{{ scope.row.configValue }}</span>
        </template>
      </el-table-column>

      <el-table-column  min-width="10%" align="center" label="类型">
        <template slot-scope="scope">
            <span>{{scope.row.configType}}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="种类">
        <template slot-scope="scope">
            <span>{{scope.row.configKind}}</span>
        </template>
      </el-table-column>
      <el-table-column  min-width="10%" align="center" label="时间">
        <template slot-scope="scope">
            <span>{{scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>
      
      <el-table-column align="center" label="操作"  min-width="15%">
        <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="handleEdit(scope.row.id)">
              编辑
            </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.rows" @pagination="getList" />

    <el-dialog title="修改配置" :visible.sync="dialog.visible"  width="700px">
        <div class="ci">
          <div class="item">
            <span class="name required">配置名称</span>
            <span class="value">
              <el-input clearable v-model="dialog.data.configName" maxlength="20" placeholder="配置名称" style="width: 220px;"></el-input>
            </span>
          </div>
          <div class="item">
            <span class="name required">值</span>
            <span class="value">
              <el-input clearable v-model="dialog.data.configValue" maxlength="100" placeholder="配置值" style="width: 220px;"></el-input>
            </span>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialog.visible = false">取 消</el-button>
            <el-button type="primary" @click="saveConfigInfo()" :loading="dialog.saveloading">确 定</el-button>
        </div>
    </el-dialog>

  </div>
</template>

<script>

import { deepClone,parseTime } from '@/utils'
import * as auth from '@/utils/auth'
import * as sysconfigApi from '@/api/sysconfig'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import guid from '@/utils/guid'


export default {
  name: 'SysConfigList',
  components: { Pagination },
  data(){
    return{
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        rows: 20,
        configName:'',
      },
      search:{
        configName:'',
      },
      dialog:{
          visible:false,
          saveloading:false,
          //数据
          data:{
            id:'',
            configName:'',
            configValue:'',
          }
      },
    }
  },
  created(){
      var that=this;
      that.getList();
  },
  mounted(){

  },
  filters:{
    
  },
  methods: {
    getList(){
      var that=this;
      that.listLoading = true;
      sysconfigApi.getSysConfigList(that.listQuery).then(res=>{
         if(res.data){
            that.list=[...res.data.items.map((item,index)=>{
              item._id=index+1;
              return item;
            })];
            that.listLoading = false;
            that.total=res.data.total || 0;
         }
      })
    },
    searchClick(){
      var that=this;
      that.listQuery.page=1;
      that.listQuery.configName=that.search.configName.trim();
      that.getList();
    },
    handleEdit(id){
        var that=this;
        that.dialog.visible=true;
        var idx=that.list.findIndex(item=>item.id==id);
        if(idx>-1){
            that.dialog.data.configName=that.list[idx].configName;
            that.dialog.data.configValue=that.list[idx].configValue;
            that.dialog.data.id=that.list[idx].id;
        }
    },
    saveConfigInfo(){
        var that=this;
        if(!that.dialog.data.configName.trim() || !that.dialog.data.configValue.trim()){
            that.$message({
                message:'请填写完整信息再提交',
                type:'warning'
            });
            return;
        }
        that.dialog.saveloading=true;
        var data={
            id:that.dialog.data.id,
            name:that.dialog.data.configName.trim(),
            value:that.dialog.data.configValue.trim(),
        }
        sysconfigApi.SaveSysConfig(data).then(res=>{
            if(res.data){
                that.$message({
                    message:'保存成功',
                    type:'success'
                });
                var idx=that.list.findIndex(item=>item.id==data.id);
                if(idx>-1){
                    that.list[idx].configName=data.name;
                    that.list[idx].configValue=data.value;
                }
            }
            setTimeout(()=>{
                that.dialog.saveloading=false;
                that.dialog.visible=false;
            },1000)
        }).catch(()=>{
            that.dialog.saveloading=false;
        })
    }
  }
}
</script>
